(function () {
    var options = {
        key1: "para1",
        originimg:'',
        canvas:''
    }

    //插入input元素且绑定click，给input添加时间触发器
    var insertDom = function(t){
        t.className = t.className+' icrop-main';
        var i = document.createElement("input");
        i.setAttribute('id','fileinput');
        i.setAttribute('type','file');
        t.appendChild(i);
        if(window.addEventListener){ // Not IE 
            t.addEventListener('click', function(){
                var evt = document.createEvent('MouseEvents');
                evt.initEvent( 'click', false, false );
                i.dispatchEvent(evt);
            }, false); 
        } else { // IE 
            t.attachEvent('onclick', function(){
                i.fireEvent('onclick');
                event.stopPropagation;
            }); 
        } 

        var d = document.createElement("canvas");
        d.setAttribute('id','icanvas');
        t.appendChild(d);
    }

    var api = {
        config: function (opts) {
            //没有参数传入，直接返回默认参数
            if(!opts) return options;
            //有参数传入，通过key将options的值更新为用户的值
            for(var key in opts) {
                options[key] = opts[key];
            }
            return this;
        },
        listen: function listen(elem) {
            //这里通过typeof设置监听的元素需为字符串调用，实际可根据需要进行更改
            if (typeof elem === 'string') {
                //这里使用ES5的querySelectorAll方法获取dom元素
                var elems = document.querySelectorAll(elem),
                    i = elems.length;
                    //通过递归将listen方法应用在所有的dom元素上
                    while (i--) {
                        listen(elems[i]);
                    }
                    return
            }
            //插入input、div元素
            insertDom(elem);
            api.readFile();
            return this;
        },
        readFile: function(){
            var self = this;
            self.ficInput = document.getElementById("fileinput");
            //self.ficImage = document.getElementById("ficImage");
            if(typeof FileReader==='undefined'){ 
                alert("抱歉，你的浏览器不支持 FileReader"); 
                input.setAttribute('disabled','disabled'); 
            }else{ 
                self.ficInput.addEventListener('change', readFile, false);
            } 
            function readFile(){
                var file = this.files[0]; 
                var reader = new FileReader(); 
                reader.readAsDataURL(file); 
                reader.onload = function(e){
                    self.drawCanvasImg(this.result);//this.result为base64编码的src
                } 
            }
        },
        drawCanvasImg: function(src){
            var self = this;
            self.originimg = new Image();
            self.originimg.src = src;
            self.canvas = document.getElementById("icanvas2"); 

            //绘制底部图片
            var ctx = self.canvas.getContext('2d');
            ctx.drawImage(self.originimg, 0, 0,300,400);
            //绘制透明图层
            //ctx.beginPath();
            // ctx.fillStyle="rgba(0,0,0,0.6)";
            // ctx.fillRect(0, 0, 300, 400);
        }
    }
    this.Icrop = api;
})();